// 订单模型
const { Orders } = require('../../../models/order');
// 评论模型
const { Comment, validateComment } = require('../../../models/comment');
// 统一数据返回类
const { DataReturn } = require('../../../common/returnclass');

module.exports = async (req, res) => {
	let { _id, content } = req.fields;
	console.log(req.fields);
	
	const { error } = validateComment(req.fields);
	// 格式不符合要求
	if (error) return DataReturn.getData(res,400,[],error.message);
	
	// 创建评论对象
	comment = new Comment({
		order_id:_id,
		content:content
	});
	
	comment.save()
	.then(async(res)=>{
		// console.log(res);
		// 评论后 待评论后改为已评价 4
		return Orders.updateOne({_id}, {state:4, is_evaluate: true, $push: { estimate: res._id } })
	})
	.then(()=>{
		DataReturn.getData(res,200,[],'评论成功')
	}).catch(()=>{
		DataReturn.getData(res,400,[],'评论失败')
	})


}